/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

import xowl.gmi.environment.Locale;

/**
 *
 * @author Laurent WOUTERS
 */
public class OverlayEditor implements Overlay {
    private Diagram diagram;
    private DataPlaceholder widget;
    private javax.swing.JTextField field;
    private javax.swing.JPanel panel;
    private javax.swing.JButton buttonOK;
    private javax.swing.JButton buttonCancel;
    private DiagramStyle style;
    
    @Override public javax.swing.JComponent getComponent() { return panel; }
    @Override public Couple getLocation() { return diagram.toDiagramCoord(new Couple(panel.getLocation())); }
    @Override public Couple getSize() { return diagram.toDiagramCoord(new Couple(panel.getSize())); }
    
    public OverlayEditor(Diagram diagram, Locale locale, DataPlaceholder widget, Couple position, DiagramStyle style, String value) {
        this.diagram = diagram;
        this.widget = widget;
        this.style = style;
        
        Couple location = diagram.toSwingCoord(position);
        Couple dim = diagram.toSwingCoord(widget.getSize());
        java.awt.Font font = widget.getFont();
        if (font != null)
            font = new java.awt.Font(font.getFontName(), font.getStyle(), (int)(font.getSize()*diagram.getZoom()));
        else
            font = new java.awt.Font(style.fontName, style.fontStyle, (int)(style.fontSize*diagram.getZoom()));
        
        this.field = new javax.swing.JTextField(value);
        this.field.setBackground(style.editableEditionColor);
        this.field.setMinimumSize(dim.toAWTDim());
        this.field.setPreferredSize(dim.toAWTDim());
        this.field.setMaximumSize(dim.toAWTDim());
        this.field.setFont(font);
        this.field.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override public void keyTyped(java.awt.event.KeyEvent evt) {
                if (evt.getKeyChar() == '\n') {
                    validate();
                } else {
                    super.keyTyped(evt);
                }
            }
        });
        this.field.requestFocus();
        
        this.buttonOK = new javax.swing.JButton(xowl.gmi.view.IconProvider.getEmbedded(locale.ok_icon));
        this.buttonOK.setToolTipText(locale.ok_icon);
        this.buttonOK.setMinimumSize(new java.awt.Dimension(20, 20));
        this.buttonOK.setPreferredSize(new java.awt.Dimension(20, 20));
        this.buttonOK.setMaximumSize(new java.awt.Dimension(20, 20));
        this.buttonOK.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                validate();
            }
        });
        
        this.buttonCancel = new javax.swing.JButton(xowl.gmi.view.IconProvider.getEmbedded(locale.cancel_icon));
        this.buttonCancel.setToolTipText(locale.cancel_icon);
        this.buttonCancel.setMinimumSize(new java.awt.Dimension(20, 20));
        this.buttonCancel.setPreferredSize(new java.awt.Dimension(20, 20));
        this.buttonCancel.setMaximumSize(new java.awt.Dimension(20, 20));
        this.buttonCancel.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                OverlayEditor.this.diagram.clearEditor();
            }
        });
        
        javax.swing.JPanel commands = new javax.swing.JPanel();
        commands.setOpaque(false);
        commands.setLayout(new javax.swing.BoxLayout(commands, javax.swing.BoxLayout.X_AXIS));
        commands.add(buttonCancel);
        commands.add(javax.swing.Box.createGlue());
        commands.add(buttonOK);
        
        this.panel = new javax.swing.JPanel();
        this.panel.setOpaque(false);
        this.panel.setLayout(new javax.swing.BoxLayout(this.panel, javax.swing.BoxLayout.Y_AXIS));
        this.panel.add(this.field);
        this.panel.add(commands);
        this.panel.setBounds((int)location.x, (int)location.y, (int)dim.x, (int)(dim.y + 20));
    }
    
    @Override public void onHooked() {
        panel.getRootPane().setDefaultButton(buttonOK);
        panel.getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0), "Cancel");
        panel.getRootPane().getActionMap().put("Cancel", new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                diagram.clearEditor();
            }
        });
        field.requestFocus();
        field.selectAll();
    }
    
    @Override public boolean validate() {
        if (widget.validateInput(field.getText())) {
            diagram.clearEditor();
            return true;
        }
        field.setBackground(style.editableErrorColor);
        return false;
    }
}
